package com.spa.infrastructure.mybatisplus;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Duke_yzl
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        this.strictInsertFill(metaObject, "createTime", Date.class, DateTime.now());
        this.strictInsertFill(metaObject, "updateTime", Date.class, DateTime.now());
        //非 web 上下文无法获取 HttpServletRequest
//        try{
//            if(StpUtil.isLogin()){
//                String userId = String.valueOf(SessionUtil.getUser().getExtUserId());
//                this.strictInsertFill(metaObject, "createBy", String.class, userId);
//                this.strictInsertFill(metaObject, "updateBy", String.class, userId);
//            }
//        } catch (Exception e){
//            log.error(" ", e);
//        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        this.strictUpdateFill(metaObject, "updateTime", Date.class, DateTime.now());
//        try{
//            String userId = String.valueOf(SessionUtil.getUser().getExtUserId());
//            this.strictUpdateFill(metaObject, "updateBy", String.class, userId);
//        } catch (Exception e){
//            log.error(" ", e);
//        }
    }

}